import Vue from 'vue'
import {Message} from "element-ui";
import VueRouter from 'vue-router'
import Teacher from '../views/Teacher.vue'
import Student from '../views/Student'
import Login from '../views/Login.vue'
import TeacherIndex from '../views/teacher/TeacherIndex.vue'
import StudentIndex from '../views/student/StudentIndex.vue'
import store from "../store"
import settings from "@/assets/js/settings";
import cookies from "vue-cookies";
import StudentList from "../views/teacher/StudentList";
import ClassList from "../views/teacher/ClassList";
import SubjectList from "../views/teacher/SubjectList";
import TitleList from "../views/teacher/TitleList";
import ShortAnswer from "../views/teacher/ShortAnswer";
import JudgeTitle from "../views/teacher/JudgeTitle";
import SingleChoice from "../views/teacher/SingleChoice";
import MultiChoice from "../views/teacher/MultiChoice";
import PaperList from "../views/teacher/PaperList";
import EditPaper from "../views/teacher/EditPaper";
import ReadPaper from "../views/ReadPaper";
import EditTask from "../views/teacher/EditTask";
import MarkPaper from "../views/teacher/MarkPaper";
import StudentExam from "../views/student/StudentExam";
import HandCheckPaper from "../views/teacher/HandCheckPaper";
import StudentCheckPaper from "../views/student/StudentCheckPaper";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'teacher',
        component: Teacher,
        children: [ // 教师子路由
            {
                name: 'TeacherIndex',
                path: '/TeacherIndex',
                component: TeacherIndex,
                children: [],
            },
            {
                name: 'StudentList',
                path: '/teacher/studentlist',
                component: StudentList,
                children: [],
            },
            {
                name: 'ClassList',
                path: '/teacher/Classlist',
                component: ClassList,
                children: [],
            },
            {
                name: 'SubjectList',
                path: '/teacher/SubjectList',
                component: SubjectList,
                children: [],
            },
            {
                name: 'TitleList',
                path: '/teacher/TitleList',
                component: TitleList,
                children: [],
            },
            {
                name: 'ShortAnswer',
                path: '/teacher/ShortAnswer',
                component: ShortAnswer,
                children: [],
            },
            {
                name: 'JudgeTitle',
                path: '/teacher/JudgeTitle',
                component: JudgeTitle,
                children: [],
            },
            {
                name: 'SingleChoice',
                path: '/teacher/SingleChoice',
                component: SingleChoice,
                children: [],
            },
            {
                name: 'MultiChoice',
                path: '/teacher/MultiChoice',
                component: MultiChoice,
                children: [],
            },
            {
                name: 'PaperList',
                path: '/teacher/PaperList',
                component: PaperList,
                children: [],
            },
            {
                name: 'EditPaper',
                path: '/teacher/EditPaper',
                component: EditPaper,
                children: [],
            },
            {
                name: 'EditTask',
                path: '/teacher/EditTask',
                component: EditTask,
                children: [],
            },
            {
                name: 'MarkPaper',
                path: '/teacher/MarkPaper',
                component: MarkPaper,
                children: [],
            },
        ]
    },
    {
        name: 'ReadPaper',
        path: '/ReadPaper',
        component: ReadPaper,
        children: [],
    },
    {
        name: 'HandCheckPaper',
        path: '/HandCheckPaper',
        component: HandCheckPaper,
        children: [],
    },
    {
        path: '/student',
        name: 'student',
        component: Student,
        children: [  // 学生子路由
            {
                path: '/StudentIndex',
                name: 'StudentIndex',
                component: StudentIndex
            },
        ]
    },
    {
        path: '/StudentExam',
        name: 'StudentExam',
        component: StudentExam
    },
    {
        path: '/StudentCheckPaper',
        name: 'StudentCheckPaper',
        component: StudentCheckPaper
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
]

const router = new VueRouter({
    routes,
    mode: 'history', //去掉路由中的#
})

router.beforeEach((to, from, next) => {
    let token = cookies.get("token")
    let userinfo = cookies.get("userinfo")
    if (to.path === '/login') {//不拦截登录
        next()
    } else if (!token || !userinfo) {  //token和用户信息不存在，跳转到登陆页面
        next({path: '/login'})
    } else {
        if (userinfo.role === 1 || userinfo.role === 2) {  // 老师和管理员不拦截
            window.onkeydown = window.onkeyup = window.onkeypress = function (event) {
                window.event.returnValue = true;
            }
            document.oncontextmenu = new Function("event.returnValue=true");
            document.onselectstart = new Function("event.returnValue=true");
            store.commit("setMenuList", settings.teacher)
            if (to.name === 'teacher') {
                next({path: '/TeacherIndex'})
            } else {
                next()
            }
        } else if (userinfo.role === 0) {  // 学生拦截
            window.onkeydown = window.onkeyup = window.onkeypress = function (event) {
                if (event.keyCode === 123) {
                    event.preventDefault(); // 阻止默认事件行为
                    window.event.returnValue = false;
                }
            }
            document.oncontextmenu = new Function("event.returnValue=false");
            document.onselectstart = new Function("event.returnValue=false");
            let permission = settings.studentPermission
            let is_permit = false
            for (let path of permission) {
                if (to.name === path) {
                    is_permit = true
                    break
                }
            }
            if (is_permit) {
                next()
            } else {
                Message({
                    type: 'warning',
                    message: '学生无权限访问该页面',
                    duration: 5000,
                })
                next({path: '/StudentIndex'})
            }
        }
    }
})

router.afterEach((to, from) => {
    document.title = to.name
})

export default router
